package com.zjd.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjd.association.entity.ActiveLogs;
import com.zjd.association.entity.Activities;
import com.zjd.association.entity.Users;
import com.zjd.association.mapper.ActiveLogsMapper;
import com.zjd.association.mapper.ActivitiesMapper;
import com.zjd.association.mapper.UsersMapper;
import com.zjd.association.service.ActiveLogsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报名记录 服务层实现类
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@Service
@RequiredArgsConstructor
public class ActiveLogsServiceImpl implements ActiveLogsService {

    private final UsersMapper usersMapper;

    private final ActiveLogsMapper activeLogsMapper;

    private final ActivitiesMapper activitiesDao;

    @Override
    public void add(ActiveLogs activeLogs) {
        Activities activities = activitiesDao.selectById(activeLogs.getActiveId());
        activities.setTotal(activities.getTotal() + 1);
        activitiesDao.updateById(activities);
        activeLogsMapper.insert(activeLogs);
    }

    @Override
    public void update(ActiveLogs activeLogs) {
        activeLogsMapper.updateById(activeLogs);
    }

    @Override
    public void delete(ActiveLogs activeLogs) {
        activeLogsMapper.deleteById(activeLogs);
    }

    /**
     * 用户是否参与活动
     *
     * @param activeId 指定活动ID
     * @param userId 指定用户ID
     * @return 结果
     */
    @Override
    public Boolean isActive(String activeId, String userId) {
        QueryWrapper<ActiveLogs> qw = new QueryWrapper<ActiveLogs>();
        qw.eq("active_id", activeId);
        qw.eq("user_id", userId);
        return activeLogsMapper.selectCount(qw) <= 0;
    }

    @Override
    public ActiveLogs getOne(String id) {
        return activeLogsMapper.selectById(id);
    }

    /**
     * 依据活动ID获取报名列表
     *
     * @param activeId 活动ID
     * @return 报名列表
     */
    @Override
    public List<Map<String, Object>> getListByActiveId(String activeId) {
        List<Map<String, Object>> res = new ArrayList<>();
        QueryWrapper<ActiveLogs> qw = new QueryWrapper<>();
        qw.eq("active_id", activeId);
        qw.orderByDesc("create_time");
        List<ActiveLogs> activeLogs = activeLogsMapper.selectList(qw);

        for (ActiveLogs activeLog : activeLogs) {
            Map<String, Object> temp = new HashMap<String, Object>();
            temp.put("id", activeLog.getId());
            temp.put("createTime", activeLog.getCreateTime());
            temp.put("activeId", activeLog.getActiveId());
            Users user = usersMapper.selectById(activeLog.getUserId());
            temp.put("userId", activeLog.getUserId());
            temp.put("userName", user.getName());
            temp.put("userGender", user.getGender());
            temp.put("userPhone", user.getPhone());
            res.add(temp);
        }

        return res;
    }
}